﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities.QuickSql;
using MySql.Data;
using System.Threading;

namespace Deviseur_Cs2i_Car
{
    public partial class AddContenance : Form
    {
        private CONTENANCE Contenance;

        public AddContenance()
        {
            InitializeComponent();

            this.Text = "Ajouter une contenance moteur";
            this.buttonAddContenance1.Text = "Ajouter";
        }
        public AddContenance(int NumContenance)
        {
            InitializeComponent();
            
            this.Text = "Modifier une contenance moteur";
            this.buttonAddContenance1.Text = "Modifier";

            Table contenance = Program.Db.QueryTable("SELECT LibelleContenance FROM CONTENANCE WHERE NumContenance = " + NumContenance);
            foreach (Record r in contenance)
            {
                this.textLibelle.Text = r.Values[0].ToString();
                Contenance = new CONTENANCE(NumContenance, this.textLibelle.Text.ToString());
            }
        }
        private void buttonAddContenance1_Click(object sender, EventArgs e)
        {
            if (textLibelle.Text.ToString() != "")
            {
                if (this.buttonAddContenance1.Text == "Ajouter")
                {
                    Contenance = new CONTENANCE(textLibelle.Text.ToString());

                    Thread addThread = new Thread(new ThreadStart(addContenance_Thread));
                    addThread.Start();
                }
                else if (this.buttonAddContenance1.Text == "Modifier")
                {
                    Contenance.set_LibelleContenance(this.textLibelle.Text.ToString());
                    Thread updateThread = new Thread(new ThreadStart(updateContenance_Thread));
                    updateThread.Start();
                }
            }
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs", "Erreur de saisie");
            }
        }
        private void addContenance_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            Contenance.new_Contenance();
            Invoke(new MethodInvoker(CloseFen));
        }
        private void updateContenance_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            Contenance.modify_Contenance();
            Invoke(new MethodInvoker(CloseFen));
        }
        private void Disabled()
        {
            this.textLibelle.Enabled = false;
            this.buttonAddContenance1.Enabled = false;
        }
        private void CloseFen()
        {
            this.Close();
        }

         
    }
}
